package com.example.appointment.controller;

import com.example.appointment.common.Result;
import com.example.appointment.dto.HealthCheckDTO;
import com.example.appointment.dto.HealthCheckStatsDTO;
import com.example.appointment.service.HealthCheckService;
import com.example.appointment.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/health-check")
@Slf4j
public class HealthCheckController {
    
    private final HealthCheckService healthCheckService;
    private final UserService userService;
    
    public HealthCheckController(HealthCheckService healthCheckService, UserService userService) {
        this.healthCheckService = healthCheckService;
        this.userService = userService;
    }
    
    @PostMapping
    public Result<HealthCheckDTO> createHealthCheck(@RequestBody HealthCheckDTO healthCheckDTO, HttpServletRequest request) {
        // 获取令牌信息
        String token = request.getHeader("Authorization");
        log.info("创建健康打卡，收到的Authorization令牌: {}", token);
        
        // 设置当前用户ID
        Long userId = userService.getCurrentUser().getId();
        log.info("创建健康打卡记录，当前用户ID: {}, 用户名: {}, 角色: {}", 
                userId, 
                userService.getCurrentUser().getUsername(),
                userService.getCurrentUser().getRole());
        healthCheckDTO.setUserId(userId);
        return Result.success(healthCheckService.createHealthCheck(healthCheckDTO));
    }
    
    @GetMapping
    public Result<Page<HealthCheckDTO>> getHealthChecks(
            @RequestParam(required = false) String type,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {
        // 获取令牌信息
        String token = request.getHeader("Authorization");
        log.info("获取健康打卡列表，收到的Authorization令牌: {}", token);
        
        Long userId = userService.getCurrentUser().getId();
        log.info("获取健康打卡记录，当前用户ID: {}, 用户名: {}, 角色: {}", 
                userId, 
                userService.getCurrentUser().getUsername(),
                userService.getCurrentUser().getRole());
        return Result.success(healthCheckService.getHealthChecks(
                userId, type, startDate, endDate, PageRequest.of(page, size)));
    }
    
    @GetMapping("/{id}")
    public Result<HealthCheckDTO> getHealthCheck(@PathVariable Long id) {
        return Result.success(healthCheckService.getHealthCheck(id));
    }
    
    @PutMapping("/{id}")
    public Result<HealthCheckDTO> updateHealthCheck(
            @PathVariable Long id,
            @RequestBody HealthCheckDTO healthCheckDTO) {
        healthCheckDTO.setUserId(userService.getCurrentUser().getId());
        return Result.success(healthCheckService.updateHealthCheck(id, healthCheckDTO));
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteHealthCheck(@PathVariable Long id) {
        healthCheckService.deleteHealthCheck(id);
        return Result.success();
    }
    
    @GetMapping("/stats")
    public Result<HealthCheckStatsDTO> getHealthCheckStats(
            @RequestParam(defaultValue = "7days") String timeRange) {
        Long userId = userService.getCurrentUser().getId();
        log.info("获取健康打卡统计，当前用户ID: {}, 用户名: {}", userId, userService.getCurrentUser().getUsername());
        return Result.success(healthCheckService.getHealthCheckStats(userId, timeRange));
    }
} 